package com.sheng.project.common;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.sql.Delete;
import org.hibernate.sql.Insert;
import org.hibernate.sql.Update;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;

/**
 * @author by ls
 * @date 2023/8/21
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@MappedSuperclass// 标注为映射超类，属性被子类继承和使用，本身不会映射到数据库中
@EntityListeners(AuditingEntityListener.class)// 关联审计监听器
public class BasicEntity implements Serializable, BasicObject {

    @ApiModelProperty(value = "主键 ID")
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "com.sheng.project.common.DefaultUUIDGenerator")
    @Column(length = 32, columnDefinition = "VARCHAR(32) NOT NULL COMMENT '主键 ID'")
    @NotEmpty(groups = {Insert.class, Delete.class, Update.class})
    @Size(max = 32, message = "主键 ID 为 32 位的字符串")
    protected String id;

    @ApiModelProperty(value = "创建时间")
    @Column(name = "created_date", columnDefinition = "DATETIME NULL COMMENT '创建时间'")
    @CreatedDate
    protected Date createdDate;

    @ApiModelProperty(value = "创建人")
    @CreatedBy
    @Column(name = "created_by", length = 100, columnDefinition = "VARCHAR(100) NULL COMMENT '创建人'")
    protected String createdBy;

    @ApiModelProperty(value = "最后修改时间")
    @Column(name = "last_modified_date", columnDefinition = "DATETIME NULL COMMENT '最后修改时间'")
    @LastModifiedDate
    protected Date lastModifiedDate;

    @ApiModelProperty(value = "最后修改人")
    @Column(name = "last_modified_by", length = 100, columnDefinition = "VARCHAR(100) NULL COMMENT '最后修改人'")
    @LastModifiedBy
    protected String lastModifiedBy;

    @ApiModelProperty(value = "删除标志")
    @Column(name = "deleted", nullable = false, columnDefinition = "BIT(1) NULL COMMENT '删除标志'")
    protected Boolean deleted = false;
}
